// pages/getinfo/getinfo.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    openid: '',
    nickname: '',
  },
  onLoad(e) {
    this.setData({
      openid: e.openid
    })
  },

  
  onNicknameInput(e) {
    this.setData({
      nickname: e.detail.value
    });
  },
 
  onLoginTap() {
    const { nickname, openid } = this.data;
    if (!nickname) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    } else {
      const db = wx.cloud.database();
      const usersCollection = db.collection('users');
      usersCollection.add({
        data: {
          openid: openid,
          nickname: nickname,
          score: 0
        },
        success: function() {
          console.log('新用户注册成功');
          wx.navigateTo({
            url: `/pages/select/select?nickname=${nickname}`,
          })
        },
        fail: function(error) {
          console.error('用户注册失败', error);
        }
      });
    }
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
  }
})